package com.woniu.controller;

import com.woniu.entity.Result;
import com.woniu.pojo.Appeal;
import com.woniu.pojo.Order;
import com.woniu.service.AppealService;
import com.woniu.service.OrderService;
import com.woniu.utils.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "申诉管理类")
@RestController
@RequestMapping("/appeal")
public class AppealController extends BaseController {

    @Autowired
    private AppealService appealService;

    @Autowired
    private OrderService orderService;

    @ApiOperation("根据订单ID查询申诉数据")
    @GetMapping("/getAppealByOrderId/{orderId}")
    public Result getAppealByOrderId(@PathVariable String orderId){
        Appeal appeal = appealService.getAppealByOrderId(orderId);
        if(appeal != null){
            return toDataResult(appeal);
        }
        return error("未查询到申诉数据");
    }

    @ApiOperation("根据申诉ID处理订单")
    @PostMapping("/handleAppealById")
    public Result handleAppealById(@RequestBody Appeal appeal){
        Order order = orderService.getById(appeal.getOrderId());
        if(appeal.getIsPass()){
            appeal.setStatus("3");
            order.setOrderStatus("7");
        }else{
            appeal.setStatus("2");
            order.setOrderStatus("6");
        }
        boolean orderFlag = orderService.updateById(order);
        boolean appealFlag = appealService.updateById(appeal);
        if(orderFlag && appealFlag){
            return success("处理成功");
        }
        return error("处理失败");
    }

}
